package com.lbd.system.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.system.bean.Message;
import com.lbd.system.dao.MessageRepository;
import com.lbd.system.service.MessageService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by zzl on 2016/9/27.
 */
@Service
@Transactional
public class MessageServiceImpl extends BaseServiceImpl<Message, MessageRepository> implements MessageService {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    MessageRepository messageRepository;

    @Override
    public Specification<Message> getSpecification(Message message) {
        return null;
    }

    /**
     * 构建消息查询条件和参数
     *
     * @param message
     * @param parameter(用户名|电话)
     * @return
     */
    @Override
    public String buildJpql(Message message, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  Message t   ");
        if (message != null) {
            if (message.getUser() != null) {
                if (StringUtils.isNotBlank(message.getUser().getName())) {
                    jpqlBuilder.append(" and  t.user.name like :name");
                    parameter.put("name", "%" + message.getUser().getName() + "%");
                }
                if (StringUtils.isNotBlank(message.getUser().getTelephone())) {
                    jpqlBuilder.append(" and  t.user.telephone like :telephone");
                    parameter.put("telephone", "%" + message.getUser().getTelephone() + "%");
                }
            }
        }
        jpqlBuilder.append(" ORDER BY t.createdDate DESC ");
        return jpqlBuilder.toString();
    }

}
